package com.kai.mqtt.controller;

import com.kai.mqtt.auth.service.IMqttUserService;
import com.kai.mqtt.data.PageResult;
import com.kai.mqtt.data.Result;
import com.kai.mqtt.data.request.MqttUserLoginRequest;
import com.kai.mqtt.data.request.MqttUserRequest;
import com.kai.mqtt.data.request.PageMqttUserRequest;
import com.kai.mqtt.data.response.MqttUserResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author weijk
 * @since 2024/8/15
 */
@RestController
@RequestMapping("/mqtt/user")
public class MqttUserController {

    @Autowired
    private IMqttUserService mqttUserService;

    /**
     * 登录
     *
     * @param request 参数
     * @return accessToken
     */
    @PostMapping("/login")
    public Result<String> login(@RequestBody @Valid MqttUserLoginRequest request) {
        return Result.success(mqttUserService.login(request));
    }

    /**
     * 登出
     *
     * @return 结果
     */
    @PostMapping("/logout")
    public Result<Boolean> logout() {
        return Result.success(mqttUserService.logout());
    }

    /**
     * 分页查询用户列表
     *
     * @param request 参数
     * @return 用户列表
     */
    @PostMapping("/page")
    public PageResult<MqttUserResponse> pageUser(@RequestBody PageMqttUserRequest request) {
        return mqttUserService.pageUser(request);
    }

    /**
     * 创建用户
     *
     * @param request 参数
     * @return 结果
     */
    @PostMapping("/create")
    public Result<Boolean> createUser(@RequestBody @Valid MqttUserRequest request) {
        return Result.success(mqttUserService.createUser(request));
    }

    /**
     * 删除用户
     *
     * @param id id
     * @return 结果
     */
    @DeleteMapping("/delete")
    public Result<Boolean> deleteUser(@RequestParam("id") Integer id) {
        return Result.success(mqttUserService.deleteUser(id));
    }
}
